package com.wang.springbootinit.controller;

import com.wang.springbootinit.common.BaseResponse;
import com.wang.springbootinit.common.ResultUtils;
import com.wang.springbootinit.model.dto.chart.GenChartByAiRequest;
import com.wang.springbootinit.model.result.BIResponseResult;
import com.wang.springbootinit.model.vo.BIResponse;
import com.wang.springbootinit.service.GenChartService;
import com.wang.springbootinit.service.impl.GenChart.GenChartAsyncServiceImpl;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


/**
 * 用作分析数据
 */
@RestController
@RequestMapping("/analyse")
public class GenChartController {

    @Resource
    private GenChartService genChartService;
    @Resource
    private GenChartAsyncServiceImpl genChartAsyncService;
    /**
     * 智能分析（同步）
     *
     * @param multipartFile
     * @param genChartByAiRequest
     * @param request
     * @return
     */
    @PostMapping("/gen")
    public BaseResponse<BIResponse> genChartByAi(@RequestPart("file") MultipartFile multipartFile,  GenChartByAiRequest genChartByAiRequest, HttpServletRequest request) throws IOException {
        BIResponseResult biResponseResult = genChartService.genChartByAi(multipartFile, genChartByAiRequest, request);
        if(!ResultUtils.isSuccess(biResponseResult)){
            return ResultUtils.error(biResponseResult.getCode(), biResponseResult.getMessage());
        }
        return ResultUtils.success(biResponseResult.getData());
    }


    /**
     * 智能分析（异步）
     *
     * @param multipartFile
     * @param genChartByAiRequest
     * @param request
     * @return
     */
    @PostMapping("/gen/async")
    public BaseResponse<BIResponse> genChartByAiAsync(@RequestPart("file") MultipartFile multipartFile, GenChartByAiRequest genChartByAiRequest, HttpServletRequest request) {
        BIResponseResult biResponseResult = genChartAsyncService.genChartByAiAsync(multipartFile, genChartByAiRequest, request);
//        BIResponseResult biResponseResult = genChartService.genChartByAiAsync(multipartFile, genChartByAiRequest, request);
        if(!ResultUtils.isSuccess(biResponseResult)){
            return ResultUtils.error(biResponseResult.getCode(), biResponseResult.getMessage());
        }
        return ResultUtils.success(biResponseResult.getData());
    }

}
